package com.andnnl.config;

import com.ibeetl.starter.BeetlSqlMutipleSourceCustomize;
import com.zaxxer.hikari.HikariDataSource;
import org.beetl.sql.core.Interceptor;
import org.beetl.sql.core.SQLManager;
import org.beetl.sql.ext.MyDebugInterceptor;
import org.beetl.sql.ext.spring4.BeetlSqlDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import javax.sql.DataSource;

@Configuration
public class MyBeetlConfig {

    private void setDsParams(HikariDataSource ds) {
        //配置Hikari连接池
//        ds.setAutoCommit(true);//update自动提交设置
        ds.setConnectionTestQuery("select 1");//连接查询语句设置
        ds.setConnectionTimeout(3000);//连接超时时间设置
        ds.setIdleTimeout(3000);//连接空闲生命周期设置
        ds.setIsolateInternalQueries(false);//执行查询启动设置
        ds.setMaximumPoolSize(3000);//连接池允许的最大连接数量
        ds.setMaxLifetime(1800000);//检查空余连接优化连接池设置时间,单位毫秒
        ds.setMinimumIdle(10);//连接池保持最小空余连接数量
//        ds.setPoolName("hikariPool");//连接池名称
    }


    @Bean(name = "baseMaster")
    @ConfigurationProperties(prefix="spring.datasource.base.master")
    public DataSource dataSourceBaseMaster() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "baseRead")
    @ConfigurationProperties(prefix="spring.datasource.base.read")
    public DataSource datasourceBaseRead() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "smsMaster")
    @ConfigurationProperties(prefix="spring.datasource.sms.master")
    public DataSource dataSourceSmsMaster() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "smsRead")
    @ConfigurationProperties(prefix="spring.datasource.sms.read")
    public DataSource datasourceSmsRead() {
        return DataSourceBuilder.create().build();
    }


    @Bean(name = "test")
    @ConfigurationProperties(prefix="spring.datasource.test")
    public DataSource datasourceTest() {
        return DataSourceBuilder.create().build();
    }

//    @Bean
//    public RestTemplate restTemplate(RestTemplateBuilder builder) {
//        return builder.build();
//    }


    /**
     * 内容模板配置
     *
     * @return
     */

    /*@Bean(initMethod = "init", name = "beetlContentTemplateConfig")
    public BeetlGroupUtilConfiguration getContentBeetlUtilConfiguration() {

        BeetlGroupUtilConfiguration beetlGroupUtilConfiguration = new BeetlGroupUtilConfiguration();
        ResourcePatternResolver patternResolver = ResourcePatternUtils.getResourcePatternResolver(new DefaultResourceLoader());
        try {

            String root = patternResolver.getResource("classpath:templates").getFile().toString();
            WebAppResourceLoader webAppResourceLoader = new WebAppResourceLoader(root);
            beetlGroupUtilConfiguration.setResourceLoader(webAppResourceLoader);

//            beetlGroupUtilConfiguration.setConfigFileResource(patternResolver.getResource("classpath:beetl.properties"));

            return beetlGroupUtilConfiguration;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    @Bean(name = "beetlViewResolver")
    public BeetlSpringViewResolver getBeetlSpringViewResolver(@Qualifier("beetlContentTemplateConfig") BeetlGroupUtilConfiguration beetlGroupUtilConfiguration) {
        BeetlSpringViewResolver beetlSpringViewResolver = new BeetlSpringViewResolver();
        beetlSpringViewResolver.setContentType("text/html;charset=UTF-8");
        beetlSpringViewResolver.setOrder(0);
        beetlSpringViewResolver.setConfig(beetlGroupUtilConfiguration);
        return beetlSpringViewResolver;
    }*/
    @Bean
    public BeetlSqlMutipleSourceCustomize beetlSqlCustomize(
            @Qualifier("baseRead") DataSource baseRead,
            @Qualifier("smsRead") DataSource smsRead) {
        return new BeetlSqlMutipleSourceCustomize() {

            @Override
            public void customize(String dataSource, SQLManager sqlManager) {
                //可以在这里添加各种扩展，指定主从等
//                System.out.println("dataSource:" + dataSource);
                sqlManager.setInters(new Interceptor[]{new MyDebugInterceptor()});
                if ("baseMaster".equals(dataSource)) {
                    BeetlSqlDataSource source = (BeetlSqlDataSource) sqlManager.getDs();
                    source.setSlaves(new DataSource[]{baseRead});
                }
                if ("smsMaster".equals(dataSource)) {
                    BeetlSqlDataSource source = (BeetlSqlDataSource) sqlManager.getDs();
                    source.setSlaves(new DataSource[]{smsRead});
                }
            }

        };
    }
}
